package org.example.lost.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.domain.PageInfoDTO;
import org.example.domain.Result;
import org.example.lost.entity.FoundInfo;
import org.example.lost.entity.vo.GetInfoVO;
import org.example.lost.entity.vo.HistoryInfoVO;
import org.example.lost.entity.vo.LostInfoVO;
import org.example.lost.service.FoundService;
import org.example.redis.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * 失物招领子模块的功能：
 * 1、发布失物招领
 * 2、查看失物招领信息
 * 3、撤销失物招领信息
 * 4、修改失物招领信息
 * 5、查询失物招领发布历史记录
 * 6、认领物品
 * @Author hxy
 * @Date 2021/3/16 22:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/core")
@Api(tags = "失物招领")
//@CrossOrigin(allowCredentials = "true",allowedHeaders="*")
public class FoundController {
    @Autowired(required = true)
    FoundService foundService;

    @Autowired
    RedisUtils redisUtils;

    @PostMapping(value = "/found")
    @ApiOperation("发布失物招领")
    public Result addFound(@RequestBody @Valid FoundInfo foundInfo){
        int result = foundService.addFound(foundInfo);
        if(result==1){
            return Result.success(null);
        }
        return Result.fail("发布失物招领失败");
    }

    @PostMapping(value = "/founds")
    @ApiOperation("列表查询失物招领")
    public Result<PageInfoDTO<FoundInfo>> getList(@RequestBody LostInfoVO lostInfoVO){

        PageInfoDTO<FoundInfo> list = foundService.getList(lostInfoVO);

        return Result.success(list);
    }

    @DeleteMapping(value = "/found/{foundId}")
    @ApiOperation("撤销失物招领")
    public Result delete(@PathVariable(name = "foundId") @ApiParam(name = "foundId",value = "拾物id") String foundId){
        int result = foundService.deleteFound(foundId);
        if(result ==1){
            return Result.success("撤销失物招领成功");
        }
        return Result.fail("撤销失物招领失败");
    }

    @PutMapping(value = "/found")
    @ApiOperation("更新失物招领")
    public Result put(@RequestBody @Valid FoundInfo foundInfo){
        int result = foundService.updateFound(foundInfo);
        if(result ==1){
            return Result.success("更新失物招领成功");
        }
        return Result.fail("更新失物招领失败");
    }


    @PostMapping(value = "/foundOne")
    @ApiOperation("查询失物招领发布历史")
    public Result<PageInfoDTO<FoundInfo>> getOne(@RequestBody HistoryInfoVO historyInfoVO){
        PageInfoDTO<FoundInfo> list = foundService.getOne(historyInfoVO);
        return Result.success(list);
    }

    @PutMapping("/found/get")
    @ApiOperation("认领物品")
    public Result<String> found(@RequestBody GetInfoVO getInfoVO, HttpServletRequest request){
        String verifyCode = (String) redisUtils.get(getInfoVO.getUserPhone());
        if(verifyCode.equals(getInfoVO.getCode())){
            String userPhone =foundService.claim(getInfoVO.getUserId(),getInfoVO.getFoundId());
            if(userPhone != null && !userPhone.equals("")){
                return Result.success(userPhone);
            }
            return Result.fail("认领失败");
        }else {
            return Result.fail("验证码错误");
        }
    }

    @PutMapping("/found/status/{foundId}/{status}")
    @ApiOperation("修改失物招领的审核状态")
    public Result<String> updateCheckStatus(
            @PathVariable(name = "foundId") @ApiParam(name = "foundId",value = "拾物id") String foundId,
            @PathVariable(name = "status") @ApiParam(name = "status",value = "审核状态") String status){
        int result=foundService.updateCheckStatus(foundId,status);
        if(result==1){
            return Result.success("审核状态修改成功");
        }
        return Result.fail("审核状态修改失败");
    }
}
